﻿<%@ Page Title="" Language="C#" MasterPageFile="~/Views/Shared/BaseMaster.Master" Inherits="System.Web.Mvc.ViewPage<TeamDoer.Models.TaskListsGridModel>" %>

<asp:Content ContentPlaceHolderID="TitleContent" runat="server">
	TeamDoer - My tasks
</asp:Content>

<asp:Content ContentPlaceHolderID="HeadContent" runat="server">
	<link href="../../Content/jquery-ui-1.8.7.custom.css" rel="stylesheet" type="text/css" />
	<script type="text/javascript">
		try {
			if (window.location.hash.substr(0, 2) === "#!") {
				window.location.href = window.location.hash.substr(2);
			}
		}
		catch (ex) { }
	</script>
</asp:Content>

<asp:Content ID="Content2" ContentPlaceHolderID="MainContent" runat="server">

	<br style="clear:both" />

	<%-- list of tasklistS --%>
    <div style="width:500px;float:left">
		<div class="workdiv">
			<div id="divTaskLists">
				<% Html.RenderPartial("TaskLists", Model); %>
			</div>
			<hr />
			<% using (Ajax.BeginForm("AddTaskList", new AjaxOptions() { UpdateTargetId = "divTaskLists", OnSuccess = "function() { $('#newListName').val(''); }" })) { %>
				<div class="small">Create a task list:</div>
				<input type="text" name="newListName" id="newListName" maxlength="50" />	<input type="submit" value="+" />
			<% } %>
		</div>
	


	
		<%-- list of tasks --%>
		<div id="divTasks">
			<%-- if "taskListId" is passed via querystring - let's prefill this list --%>
			<% if (Model.SelectedListTasks != null) { %>
				<% Html.RenderPartial("Tasks", Model.SelectedListTasks); %>
			<% } %>
		</div>
	</div>

	
	<%-- task detailed view --%>
	<div style="float:left;width:500px" id="divTaskDetails">
		<%-- if "taskId" is passed via querystring - let's prefill this list --%>
		<% if (Model.SelectedListTasks != null && Model.SelectedListTasks.SelectedTaskDetails != null) { %>
			<% Html.RenderPartial("TaskDetails", Model.SelectedListTasks.SelectedTaskDetails); %>
		<% } %>
	</div>

	<br style="clear:both" />
	
	<%-- task editor dialog (hidden)--%>
	<div id="divEditTask" class="jqmWindow popupdiv"><fieldset>
	<legend>Edit task</legend>
		<label>* Subject:</label><input name="EditTaskSubject" id="EditTaskSubject" type="text" /><br />
		<label>Body (optional):</label><textarea name="EditTaskBody" id="EditTaskBody" rows="2"></textarea><br />
		<button type="button" id="btnSaveTask">Save</button>
		or <a href="javascript:void(0)" onclick="$('#divEditTask').jqmHide();">cancel</a>
	</fieldset></div>


	<%-- task assign dialog (hidden) --%>
	<div id="divAssignTask" class="jqmWindow popupdiv"><fieldset>
	<legend>Assign task</legend>
		<select name="assignToUserId" id="assignToUserId"></select><br />
		<button type="button" id="btnAssignTask">Assign</button>
		or <a href="javascript:void(0)" onclick="$('#divAssignTask').jqmHide();">cancel</a>
	</fieldset></div>

</asp:Content>

<asp:Content ContentPlaceHolderID="ScriptsContainer" runat="server">
	<script src="<%= Url.Content("~/Scripts/jqModal.js") %>" type="text/javascript"></script>
	<script src="<%= Url.Content("~/Scripts/jquery-ui-1.8.7.custom.min.js") %>" type="text/javascript"></script>
	<script src="<%= Url.Content("~/Scripts/MicrosoftAjax.js") %>" type="text/javascript"></script>
    <script src="<%= Url.Content("~/Scripts/MicrosoftMvcAjax.js") %>" type="text/javascript"></script>
	<script type="text/javascript">		
		<%-- shows the new task div--%>
		function ShowNewTask() {
			$('#divAddTask').jqm();
			$('#divAddTask').jqmShow();
			<%--datepicker init--%>
			$(function () {
				$(".datepick").datepicker({ gotoCurrent: true, dateFormat: "yy-mm-dd", firstDay: 1 });
			});
		}
		function HideNewTask() {
			$('#divAddTask').jqmHide();
			$('textarea[name=NewTaskBody]').val('');
			$('input[name=NewTaskDueDate]').val('');
			$('input[name=NewTaskSubject]').val('');
		}
		function ConfirmDelete(e) {
			if (!confirm("Are you sure?")) e.preventDefault();
		}
		<%--shows the task due date editor--%>
		function ShowDueDateEditor(boolShow) {
			if (boolShow === true) {
				$("#dueDateForm").show();
				$("#lnkDueDate").hide();
				$(function () {
					$(".datepick").datepicker({ gotoCurrent: true, dateFormat: "yy-mm-dd", firstDay: 1 });
				});
			}
			else {
				$("#dueDateForm").hide();
				$("#lnkDueDate").show();
			}
		}
		function ShowTaskEditor(taskId) {
			$('#EditTaskSubject').val($('#hTaskText').text());
			$('#EditTaskBody').val($('#spanTaskBody').text());
			$('#btnSaveTask').click(function() { UpdateTask(taskId); });
			$('#divEditTask').jqm();
			$('#divEditTask').jqmShow();
		}
		function UpdateTask(taskId) {
			var task = {};
			task.TaskId = taskId;
			task.Subject = $('#EditTaskSubject').val();
			task.Body = $('#EditTaskBody').val();
			$.getJSON("/Tasks/UpdateTask", task, function() {
				$('#hTaskText').text($('#EditTaskSubject').val());
				$('#spanTaskBody').text($('#EditTaskBody').val());
				$('#divEditTask').jqmHide();
			});
		}
		function RefreshFollowers(taskId) {
			$.get('/Tasks/RefreshTaskFollowers?taskId='+taskId,
				function(data) {
					$('#tdTaskFollowers').html(data);
				});
		}
		function ShowTaskAssignDialog(taskListId, taskId) {
			var taskList = {};
			taskList.TaskListId = taskListId;
			$.getJSON("/Tasks/GetTaskListUsers", taskList, function(data) {
				$('#assignToUserId').empty();
				$.each(data, function(key, val) {
					$('#assignToUserId').append($("<option></option>").attr("value",key).text(val));
				});
				$('#divAssignTask').jqm();
				$('#divAssignTask').jqmShow();
			});
			$('#btnAssignTask').click(function() {
				AssignTask(taskId, $('#assignToUserId').val());
				$('#spanAssignee').text($("#assignToUserId option:selected").text());
				RefreshFollowers(taskId);
			});
		}
		function AssignTask(taskId, userId) {
			var json = {};
			json.TaskId = taskId;
			json.UserId = userId;
			$.getJSON("/Tasks/AssignTask", json, function() { $('#divAssignTask').jqmHide(); });
		}
		function SetURLForTask(taskId) {
			SetAddressBarURL('/Tasks/TaskById?taskId=' + taskId);
		}
		function SetURLForTaskList(taskListId) {
			SetAddressBarURL('/Tasks/TasksByListId?taskListId=' + taskListId);
		}
		function SetAddressBarURL(urlToSet) {
			try {
				window.history.pushState(null, urlToSet, urlToSet);
			}
			catch(ex) {
				window.location.hash = '#!' + urlToSet;
			}
		}
	</script>
</asp:Content>
